package ${project.group}.${project.name}.vehicle;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chenenyu.router.Router;
import com.chenenyu.router.annotation.InjectParam;
import com.chenenyu.router.annotation.Route;
import com.idogfooding.backbone.network.ApiException;
import com.idogfooding.backbone.network.PageResult;
import com.idogfooding.backbone.ui.AndroidBug5497Workaround;
import ${project.group}.${project.name}.Const;
import ${project.group}.${project.name}.R;
import ${project.group}.${project.name}.base.FormActivity;
import ${project.group}.${project.name}.network.Api;
import ${project.group}.${project.name}.network.ApiCallback;
import ${project.group}.${project.name}.network.ApiHttpResult;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.kongzue.dialog.listener.OnMenuItemClickListener;
import com.kongzue.dialog.v2.BottomMenu;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;


import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 添加车辆
 *
 * @author Charles
 */
@Route(value = {"VehicleAdd", "VehicleEdit"}, interceptors = "Login")
public class VehicleAddActivity extends FormActivity implements OnDateSetListener {

    @InjectParam
    Vehicle data;
    @InjectParam
    String viewType = Const.ViewType.ADD;

    @BindView(R.id.et_item0)
    EditText etItem0;
    @BindView(R.id.et_item1)
    EditText etItem1;
    @BindView(R.id.et_item2)
    EditText etItem2;
    @BindView(R.id.et_item3)
    EditText etItem3;
    @BindView(R.id.et_item4)
    EditText etItem4;
    @BindView(R.id.et_item5)
    EditText etItem5;
    @BindView(R.id.et_item6)
    EditText etItem6;


    private TimePickerDialog dialogAll;

    @Override
    protected int getLayoutId() {
        return R.layout.vehicle_add;
    }

    @Override
    protected void onSetup(Bundle savedInstanceState) {
        this.showToolbar = true;
        super.onSetup(savedInstanceState);
        AndroidBug5497Workaround.assistActivity(this);
        Router.injectParams(this);
        fillViews();
    }
    private void fillViews() {
        initPhotoPicky();
        if (Const.ViewType.ADD.equalsIgnoreCase(viewType)) {
            toolbar.setTitleMainText("新增");
            if (null == data) {
                data = new Vehicle();
            }
            photoAdapter.setNewData(null);
        } else if (Const.ViewType.EDIT.equalsIgnoreCase(viewType)) {
            toolbar.setTitleMainText("编辑");
            setTitle(data.getVehicleNo());
            etItem0.setText(data.getVehicleNo());
            etItem1.setTag(data.getCatCd());
            etItem2.setText(data.getDriverName());
            etItem3.setText(data.getMobile());

        }
    }

    @OnClick({R.id.btn_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_submit:
                attemptSubmit(view);
                break;

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (resultCode == RESULT_OK && (requestCode == 1001 || requestCode == 1002)) {

        } else {
            super.onActivityResult(requestCode, resultCode, intent);
        }
    }

    protected void attemptSubmit(View v) {
        String vehicleNo = etItem0.getText().toString().trim().toUpperCase();
        String mobile = etItem3.getText().toString().trim();
        String driverName = etItem2.getText().toString().trim();
        if (StringUtils.isEmpty(vehicleNo) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(driverName)) {
            ToastUtils.showLong("车牌 司机 联系电话均不能为空");
        } else {
            data.setDriverName(driverName);
            data.setMobile(mobile);
            data.setVehicleNo(vehicleNo);
        }
        KeyboardUtils.hideSoftInput(v);
        OkGo.<ApiHttpResult<Vehicle>>post(Api.VEC_SAVE)
                .tag(this)
                .execute(new ApiCallback<ApiHttpResult<Vehicle>>(this, true) {
                    @Override
                    public void onSuccessData(Response<ApiHttpResult<Vehicle>> response, ApiHttpResult<Vehicle> result) {
                        if (result.getCode() == 0) {
                            ToastUtils.showLong("处理成功");
                            setResult(Activity.RESULT_OK);
                            finish();
                        } else {
                            ToastUtils.showLong("处理失败:" + result.getMsg());
                        }
                    }

                    @Override
                    protected void onApiError
                            (Response<ApiHttpResult<Vehicle>> response,
                             ApiException exception) {
                        if (exception.getCode() == 500) {
                            super.onApiError(response, exception);
                        }
                    }
                });
    }

    public void onTypeChooseClick(View view) {
        BottomMenu.show(this, new String[]{"新型渣土车", "轻型自卸货车", "其他"}, new OnMenuItemClickListener() {
            @Override
            public void onClick(String text, int index) {
                //返回参数 text 即菜单名称，index 即菜单索引
                etItem1.setText(text);
            }

        });


    }



    public void onGpsTimeChooseClick(View view) {

        dialogAll = new TimePickerDialog.Builder()
                .setTitleStringId("请选择时间")//标题
                .setThemeColor(getResources().getColor(R.color.timepicker_dialog_bg))
                .setType(Type.ALL)
                .setCallBack(this)
                .build();
        dialogAll.show(getSupportFragmentManager(), "所有");

    }

    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        Date date = new Date(millseconds);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//精确到分钟
        String time = format.format(date);
        etItem4.setText(time);
    }
}
